package xin.marcher.module.common.utils;

import xin.marcher.module.common.constants.CoreConstant;
import xin.marcher.module.common.constants.DateConstant;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * java.util.Date日期格式化工具类
 */
public class DateFormatUtil {

    private static DateFormat dateFormat = new SimpleDateFormat(CoreConstant.DATE_FORMAT_PATTERN);

    private static DateFormat dateTimeFormat = new SimpleDateFormat(CoreConstant.DATE_TIME_FORMAT_PATTERN);

    private static DateFormat timeFormat = new SimpleDateFormat(CoreConstant.TIME_FORMAT_PATTERN);

    private static DateFormat dateTimeFormatStr = new SimpleDateFormat(DateConstant.DATE_TIME_FORMAT_PATTERN);

    /**
     * 日期格式化(不包含时间) yyyy-MM-dd
     *
     * @param date
     * @return
     */
    public static String format(Date date) {
        return dateFormat.format(date);
    }

    /**
     * 日期格式化(包含时间) yyyy-MM-dd HH:mm:ss
     *
     * @param date
     * @return
     */
    public static String formatDateTime(Date date) {
        return dateTimeFormat.format(date);
    }

    /**
     * 日期格式化(包含时间) yyyyMMddHHmmss
     *
     * @return
     */
    public static String formatDateTime() {
        return dateTimeFormatStr.format(new Date());
    }

    /**
     * 时间格式化(不包含年月日) HH:mm:ss
     *
     * @param date
     * @return
     */
    public static String formatTime(Date date) {
        return timeFormat.format(date);
    }

    /**
     * 日期格式化
     *
     * @param date    日期
     * @param pattern 自定义格式
     * @return
     */
    public static String format(Date date, String pattern) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        return simpleDateFormat.format(date);
    }

    /**
     * 获取day天之后的日期
     *
     * @param day 天数
     * @return
     */
    public static String getDate(int day) {
        Calendar calendar1 = Calendar.getInstance();
        calendar1.add(Calendar.DATE, day);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(calendar1.getTime());
    }

    public static Date StringToDate(String dateStr) throws Exception {
        return dateFormat.parse(dateStr);
    }

    /**
     * 获取day小时之后的日期
     *
     * @param day 小时
     * @return
     */
    public static String getHoursDate(int day) {
        Calendar calendar1 = Calendar.getInstance();
        calendar1.add(Calendar.HOUR_OF_DAY, day);
        SimpleDateFormat sdf = new SimpleDateFormat(CoreConstant.TIMESTAMP_FORMAT_PATTERN);
        return sdf.format(calendar1.getTime());
    }

    /**
     * 获取day分钟之后的日期
     *
     * @param date 字符串时间
     * @param day  分钟
     * @return
     */
    public static String getMinuteDate(String date, int day) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(CoreConstant.TIMESTAMP_FORMAT_PATTERN);
            Date createDate = sdf.parse(date);
            Calendar calendar1 = Calendar.getInstance();
            calendar1.setTime(createDate);
            calendar1.add(Calendar.MINUTE, day);
            return sdf.format(calendar1.getTime());
        } catch (Exception e) {

        }
        return date;
    }


    /**
     * 验证时间是否超过这个 当前时间-有效时间
     *
     * @param startDate 创建时间
     * @param minutes   分钟
     * @return
     */
    public static Boolean compareTo(Date startDate, Integer minutes) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.MINUTE, minutes);
        if (startDate.getTime() > calendar.getTime().getTime()) {
            return false;
        }
        return true;
    }
}